package com.kt.delivery.controller;

import com.kt.delivery.domain.R;
import com.kt.delivery.domain.vo.WxOrderVo;
import com.kt.delivery.mapper.OrderMapper;
import com.kt.delivery.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/order")
public class OrderController {

    private final OrderService orderService;


    /**
     * 返回 快递规格（小件 大件 超大件）对应的金额
     * @param name
     * @return
     */
    @GetMapping("/getMoney")
    public BigDecimal getMoney(String name){
        if (name.equals("小件")){
            return BigDecimal.valueOf(1.2);
        }else if (name.equals("大件")){
            return BigDecimal.valueOf(3);
        }else if (name.equals("超大件")){
            return BigDecimal.valueOf(5);
        }else {
            return BigDecimal.valueOf(0);
        }

    }
    /**
     * @Description: 小程序下单
     * @Param: [userId, orders]
     * @return: java.lang.Object
     * @Author: XQD
     * @Date:2022/8/13 18:48
     */
    @PostMapping("/submitOrder")
    public R<Map<String,Object>> submitOrder(@RequestBody List<WxOrderVo> wxOrderVo) throws Exception {
        return R.ok(orderService.createOrder(wxOrderVo));
    }

    /**
     * 支付回调结果
     * @param request
     * @return
     */
    @PostMapping("/notifyUrl")
    public R notifyUrl(HttpServletRequest request) throws IOException {
        return R.ok(orderService.payNotify(request));
    }

    /**
     * 充值金币
     * @return
     * @throws Exception
     */
    @PostMapping("/updateMoney")
    public R<Map<String,Object>> updateMoney(@RequestBody Map<String,Object> map) throws Exception {
        return R.ok(orderService.updateMoney((Integer) map.get("jm"), (Integer) map.get("money")));
    }

    /**
     * 金币支付回调结果
     * @param request
     * @return
     */
    @PostMapping("/notifyUrlJm")
    public R payNotifyJm(HttpServletRequest request) throws IOException {
        return R.ok(orderService.payNotifyJm(request));
    }
}
